package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.ResponseBean;
import com.pojo.Alert;
import com.pojo.MobanFm;
import com.pojo.MobanZm;
import com.service.AlertService;
import com.service.MobanFmService;
import com.service.MobanZmService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/mobanZm")
public class MobanZmAction {

    @Autowired
    private MobanZmService mobanZmService;

    @Autowired
    private MobanFmService mobanFmService;

    @Autowired
    private AlertService alertService;

    @Value("${upload.dir}")
    private String upload;

    @Value("${upload.tmp.dir}")
    private String tmp;

    /**
     * 分页查询模板正面
     * @param page  页数
     * @param limit 条数
     * @param name  模板名称
     * @return
     */
    @RequestMapping("/selectMobanZm")
    public ResponseBean selectMobanZm(int page, int limit, String name){
        QueryWrapper query = new QueryWrapper();
        if(StringUtil.isNotNull(name)){
            query.like("name",name);
        }
        Page p = new Page(page,limit);
        IPage<MobanZm> iPage = mobanZmService.page(p,query);
        return new ResponseBean(0,iPage.getTotal(),"",iPage.getRecords());
    }

    /**
     * 查询所有正面模板
     * @return
     */
    @RequestMapping("/selectMobanZms")
    public ResponseBean selectMobanZms(String id){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        List<MobanZm> zmList = mobanZmService.list(new QueryWrapper<MobanZm>().eq("status" , 1));
        List<MobanFm> fmList = mobanFmService.list(new QueryWrapper<MobanFm>().eq("status" , 1));
        resultMap.put("zm",zmList);
        resultMap.put("fm",fmList);
//        UserInfo userInfo = userInfoService.getById(id);
        return new ResponseBean(0,"",resultMap);
    }

    /**
     * 新增正面模板
     * @param mobanZm  正面模板
     * @return
     */
    @RequestMapping("/addMobanZm")
    public ResponseBean addMobanZm(@RequestBody MobanZm mobanZm){
        if(StringUtil.isNull(mobanZm.getBg())){
            return new ResponseBean(3,1,"模板背景不能为空");
        }
        long i = mobanZmService.nameCount(mobanZm.getName());
        if(i>0){
            return new ResponseBean(2,1,"该模板名称已存在！请重新输入");
        }
        if(mobanZmService.save(mobanZm)){
            FileUtil.copy(tmp + mobanZm.getBg(), upload, true) ;
            return new ResponseBean(0,1,"新增成功");
        }
        return new ResponseBean(1,1,"新增失败");
    }

    /**
     * 修改正面模板
     * @param mobanZm  正面模板
     * @return
     */
    @RequestMapping("/updMobanZm")
    public ResponseBean updMobanZm(@RequestBody MobanZm mobanZm){

        if(StringUtil.isNull(mobanZm.getBg())){
            return new ResponseBean(3,1,"模板背景不能为空");
        }
        long x = mobanZmService.nameCount(mobanZm.getName());
        long y = mobanZmService.nameIdCount(mobanZm.getId(),mobanZm.getName());
        if(x>=1 && y==0){
            return new ResponseBean(2,1,"该模板名称已存在！请重新输入");
        }
        MobanZm moban = mobanZmService.selectID(mobanZm.getId());
        if(mobanZmService.updateById(mobanZm)){
            if(StringUtil.isNull(moban.getBg()) || moban.getBg().equals(mobanZm.getBg())==false){
                FileUtil.copy(tmp + mobanZm.getBg(), upload, true) ;
            }
            return new ResponseBean(0,1,"修改成功");
        }
        return new ResponseBean(1,1,"修改失败");
    }

    /**
     * 修改正面模板状态
     * @param id     模板ID
     * @param status 模板状态
     * @return
     */
    @RequestMapping("/updMobanZmStatus")
    public ResponseBean updMobanZmStatus(String id, int status){

        MobanZm mobanZm = new MobanZm();
        mobanZm.setId(id);
        mobanZm.setStatus(status);
        if(mobanZmService.updateById(mobanZm)){
            return new ResponseBean(0,1,"修改状态成功");
        }
        return new ResponseBean(1,1,"修改状态失败");
    }

    /**
     * 提交不合格反馈信息
     * @return
     */
    @RequestMapping("/submitFk")
    public ResponseBean submitFk(String uid, String content){
        Alert alert = new Alert();
        alert.setUid(uid);
        alert.setTitle("提示");
        alert.setYes("我知道了");
        alert.setContent(content);
        alert.setStatus("1");
        alert.setTime(new Date());
        if (alertService.save(alert)) {
            return new ResponseBean(200,1,"反馈成功");
        }
        return new ResponseBean(500,1,"提交失败");
    }
}